package com.zlll.winner.controller.advertise;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.advertising.AdvertisingPublish;
import com.zlll.winner.business.service.advertising.IAdvertisingDetailTypeService;
import com.zlll.winner.business.service.advertising.IAdvertisingPublishService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.common.BaseResult;
import com.zlll.winner.enums.AdvertiseEnum;
import com.zlll.winner.enums.OperationEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 广告管理
 */
@RestController
@Slf4j
@LoginUser
@RequestMapping(value = "/advertise/general")
public class AdvertiseGeneralController extends BaseController {

    @Reference
    private IAdvertisingPublishService advertisingPublishService;
    @Reference
    private IAdvertisingDetailTypeService advertisingDetailTypeService;

    /**
     * 分页查询
     * @param data
     * @return
     */
    @PostMapping("list")
    public BaseResult getAdPublish(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Integer pageNo = data.getInteger("pageNo");
        Integer pageSize = data.getInteger("pageSize");
        Long spaceId = data.getLong("spaceId");
        Integer type = data.getInteger("type");//1 表示首页 2 表示论坛
        Integer category = data.getInteger("category"); // 1表示普通广告 2 轮播广告
        Integer status = data.getInteger("status");
        String cityCode = data.getString("cityCode");
        IPage<Map<String, Object>> pages = advertisingPublishService.findAdvertisePage(spaceId, status, cityCode, type, category,pageNo, pageSize);
        baseResult.setData(pages);
        return baseResult;
    }

    /**
     * 新增广告
     * @param data
     * @return
     */
    @PostMapping("add")
    public BaseResult addAdPublish(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        AdvertisingPublish ad = data.toJavaObject(AdvertisingPublish.class);
        Assert.notNull(ad.getSpaceId(),"广告位Id不能为空");
        JSONArray citys = null;
        if(ad.getScope().intValue() == AdvertiseEnum.ScopeEnum.CITY.val()){
            //表示城市相关数据
            citys = data.getJSONArray("cityList");
            if (citys == null || citys.size() == 0) {
                baseResult.setMsg("发布城市不能为空");
                baseResult.setSuccess(false);
                return baseResult;
            }
        }
        JSONArray details = data.getJSONArray("detailList");
        if (details == null || details.size() == 0) {
            baseResult.setMsg("广告详情不能为空");
            baseResult.setSuccess(false);
            return baseResult;
        }
        boolean result = advertisingPublishService.addAdPublish(ad, details, citys, getUser());
        if (result) {
            baseResult.setMsg("新增广告失败");
            baseResult.setSuccess(false);
            return baseResult;
        }
        return baseResult;
    }

    /**
     * 发布/下架/删除
     * @param data
     * @return
     */
    @PostMapping("update")
    public BaseResult updateAdPublish(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long id = data.getLong("id");
        Assert.notNull(id,"广告Id不能为空");
        String operation = data.getString("operation");
        if (OperationEnum.DELETED.getValue().equals(operation)) {
            log.info("进入删除广告方法，当前传入参数为：{}", data);
            int result = advertisingPublishService.deleteAdVertise(id, getUser().getName());
            if (result == 0) {
                baseResult.setMsg("广告删除失败,当前广告id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        if (OperationEnum.OBTAINED.getValue().equals(operation)) {
            log.info("进入下架广告方法，当前传入参数为：{}", data);
            int result = advertisingPublishService.updateAdvertiseStatus(id, AdvertiseEnum.PublishEnum.OBTAINED.val(), getUser());
            if (result == 0) {
                baseResult.setMsg("广告下架失败,当前广告id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        if (OperationEnum.UP.getValue().equals(operation)) {
            log.info("进入上架架广告方法，当前传入参数为：{}", data);
            int result = advertisingPublishService.updateAdvertiseStatus(id, AdvertiseEnum.PublishEnum.PUBLISH.val(), getUser());
            if (result == 0) {
                baseResult.setMsg("广告上架失败,当前广告id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        baseResult.setMsg("参数operation的值需为delete或者obtained");
        baseResult.setSuccess(false);
        return baseResult;
    }

    /**
     * 编辑前获取广告对应的信息和项目
     * @param id
     * @return
     */
    @GetMapping("edit/get/{id}")
    public Object getPublishTree(@PathVariable Long id) {
        return advertisingPublishService.getPublishTree(id);
    }

    /**
     * 编辑功能
     * @param data
     * @return
     */
    @PostMapping("edit")
    public BaseResult editAdPublish(@RequestBody JSONObject data) {
        log.info("进入编辑广告方法，当前传入参数为：{}", data);
        BaseResult baseResult = new BaseResult();
        AdvertisingPublish ad = data.toJavaObject(AdvertisingPublish.class);
        Assert.notNull(ad.getId(),"广告Id不能为空");
        JSONArray details = data.getJSONArray("detailList");
        if (details == null || details.size() == 0) {
            baseResult.setMsg("广告详情不能为空");
            baseResult.setSuccess(false);
            return baseResult;
        }
        JSONArray citys = null;
        if(ad.getScope().intValue()==AdvertiseEnum.ScopeEnum.CITY.val()){
            //表示城市
            citys = data.getJSONArray("cityList");
            if (citys == null || citys.size() == 0) {
                baseResult.setMsg("发布城市不能为空");
                baseResult.setSuccess(false);
                return baseResult;
            }
        }
        advertisingPublishService.editAdPublish(ad, details, citys, getUser());
        return baseResult;
    }

}
